package com.atguigu.spzx.product.controller;


import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.h5.IndexVo;
import com.atguigu.spzx.product.service.CategoryService;
import com.atguigu.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "首页数据加载")
@RestController
@RequestMapping("/api/product/index")
//@CrossOrigin //临时，解决跨域问题
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    ProductSkuService productSkuService;

    @Operation(summary = "首页一级分类和畅销商品查询")
    @GetMapping
    public Result<IndexVo> index(){

        List<Category> categoryList = categoryService.findOneCategory();
        List<ProductSku> productSkuList = productSkuService.findProductSkuBySaleNum();
        IndexVo vo = new IndexVo();
        vo.setCategoryList(categoryList); //封装一级分类集合
        vo.setProductSkuList(productSkuList); //封装畅销商品集合

        return Result.build(vo, ResultCodeEnum.SUCCESS);
    }
}
